#include "Animation.h"
#include <iostream>

Animation::Animation(void)
{
}


Animation::~Animation(void)
{
}

void Animation::Update(float dt)
{
	if(Playing == false)
		return;

	animation.TimeWaited += dt * PlaybackSpeed;
	if( animation.TimeWaited > Frames[animation.CurrFrame].fDuration )
	{
		animation.TimeWaited = 0.0f;
		++animation.CurrFrame;

		// Is this the end?
		if( IsOver() == true )
		{
			if( IsLooping == true )
				animation.CurrFrame = 0;
			else 
			{
				// Stop on the last valid frame
				Playing = false;
				--animation.CurrFrame;
			}
		}
	} 
}

bool Animation::IsOver() const
{
	if( animation.CurrFrame == Frames.size() )
	{
		return true;
	}
	else
		return false;
}

void Animation::Play(bool bIsLooping, float nSpeed)
{
	IsLooping = bIsLooping;
	PlaybackSpeed = nSpeed;
	Playing = true;
}

void Animation::Play(bool bIsLooping)
{
	IsLooping = bIsLooping;
	Playing = true;
}

void Animation::Play()
{
	Playing = true;
}

void Animation::Pause()
{
	Playing = !Playing;
}

void Animation::Stop()
{
	Playing = false;
}

void Animation::Restart()
{
	Playing = false;
	Playing = true;
}

void Animation::Render( int X, int Y, bool Flipped, float Scale, DWORD dwColor )
{
	RECT rFrame;
	//source rect
	rFrame.right = Frames[animation.CurrFrame].SrcRect.right;  
	rFrame.bottom = Frames[animation.CurrFrame].SrcRect.bottom;
	rFrame.left = Frames[animation.CurrFrame].SrcRect.left;
	rFrame.top = Frames[animation.CurrFrame].SrcRect.top;

	//anchor point
	POINT Anchorpt;	
	Anchorpt.x = Frames[animation.CurrFrame].Anchorpoint.x; 
	Anchorpt.y = Frames[animation.CurrFrame].Anchorpoint.y; 

	float ScaleX = Scale;

	if( Flipped == true )
		ScaleX = -ScaleX; // flipping the image

	CSGD_TextureManager::GetInstance()->Draw(TextureID, (int)(X - (Anchorpt.x * ScaleX)),(int)(Y - (Anchorpt.y * Scale)),ScaleX,Scale,&rFrame, 0.0f, 0.0f, 0.0f, dwColor );
}

bool Animation::Initialize(char* szFilename,vector<Animation *> ani)
{
	TiXmlDocument doc;

	// Attempt to load from the file
	if( doc.LoadFile( szFilename ) == false )
		return false;

	TiXmlElement* Root = doc.RootElement();
	if(Root == nullptr)
		return false;
	char imgname[32]; 
	char rst[32];
	strcpy_s(rst,"../resource/Graphics/");
	strcpy_s(imgname,Root->Attribute("Name"));
	strcat_s(rst,imgname);
	TCHAR img[32];
	MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,(LPCSTR)rst,32,img,32); // char to TCHAR convertor
	TextureID = CSGD_TextureManager::GetInstance()->LoadTexture(img);
	TiXmlElement* anime = Root->FirstChildElement( "Frames");
	TiXmlElement * frame = anime->FirstChildElement("Frame");
	   while(frame != nullptr)
	   {
		   Frame info = {};
		   strcpy_s(info.trigger,frame->Attribute("Trigger"));
		   frame->Attribute("Duration",&info.fDuration);
		   frame->Attribute("Y",&info.Anchorpoint.y);
		   frame->Attribute("X",&info.Anchorpoint.x);
		   frame->Attribute("Right",&info.SrcRect.right);
		   frame->Attribute("Left",&info.SrcRect.left);
		   frame->Attribute("Bottom",&info.SrcRect.bottom);
		   frame->Attribute("Top",&info.SrcRect.top);
		   Frames.push_back(info);
		   frame = frame->NextSiblingElement("Frame");
	   }
	   ani.push_back(this);
	return true;

}